<?php
/**
 *泰外交部送件转认证
*/
namespace app\admin\controller;
use app\admin\controller\Admin;
use think\facade\Db;
use xhadmin\service\admin\record\CustomerService;
use xhadmin\service\admin\MemberService;
use xhadmin\db\Customer as CustomerDb;
use think\facade\Cache;

class Stepone extends Admin {


	/*会员管理*/
	function index(){
        if (!$this->request->isAjax()){
			return $this->display('index');
		}else{
            $limit  = $this->request->post('limit', 0, 'intval');
			$offset = $this->request->post('offset', 0, 'intval');
			$page   = floor($offset / $limit) +1 ;

			$id_card= $this->request->param('id_card', '', 'strip_tags,trim');
			$first_name = $this->request->param('first_name', '', 'strip_tags,trim');
			$birth_year = $this->request->param('birth_year', '', 'strip_tags,trim');
			$last_name = $this->request->param('last_name', '', 'strip_tags,trim');
            $id= $this->request->param('id', '', 'strip_tags,trim');

            if(!empty($id)){
                $where['id'] = ['=',$id];
            }
            if(!empty($id_card)){
                $where['id_card'] = ['like','%'.$id_card.'%'];
            }
            if(!empty($birth_year)){
                $where['birth_day'] = ['like','%'.$birth_year.'%'];
            }
            if(!empty($first_name)){
                $where['first_name|last_name|passport_info'] = ['like','%'.$first_name.'%'];
            }
            if(!empty($last_name)){
                $where['last_name'] = ['like','%'.$last_name.'%'];
            }

            $where['status'] = ['=',1];
			$limit = ($page-1) * $limit.','.$limit;
            //p($where);exit;
			try{
				$res = CustomerService::pageListA(formatWhere($where),$limit,'*',$orderby = 'id desc',1);
				foreach ($res['list'] as $key=>&$val) {
				    $val['work_year_month'] = $val['work_year'].'年'.$val['work_month'].'个月'.$val['work_times'].'次';
                    $val['name'] = $val['first_name'].' '.$val['last_name'];
                    $val['create_time'] = date('Y-m-d',$val['create_time']);
                }
				$list = $res['list'];
			}catch(\Exception $e){
				exit($e->getMessage());
			}

			$data['rows']  = $list;
			$data['total'] = $res['count'];
			return json($data);
		}
	}



	/*修改*/
	function update(){
		if (!$this->request->isPost()){
			$id = $this->request->get('id','','intval');
			if(!$id) $this->error('参数错误');
			$this->view->assign('info',checkData(CustomerDb::getInfo($id)));
			return $this->display('record/customer/info');
		}else{
            $postField = 'id,status,id_card,idcardimg,idcardimgel,bankimg,wjbhzimg,first_name,last_name,birth_day,
			religion,addr,expiry_date,id_card_upside_no,work_year,work_month,mobile,director,is_rename,is_hz_dg,
			is_yhb_dg,is_introducer,introducer,id_card_fee,pass_port_fee';
			$data = $this->request->only(explode(',',$postField),'post');
			try {
                CustomerService::update($data);
                if(!empty($data['pass_port_fee'])) {
                    Db::name("turnover")->where(['related_index'=>$data['id'],'subject'=>20])->update(['amount'=>$data['pass_port_fee']]);
                    Db::name("profit_loss_log")->where(['related_index'=>$data['id'],'subject'=>20])->update(['outcome_amount'=>$data['pass_port_fee']]);
                }
                if(!empty($data['id_card_fee'])) {
                    Db::name("turnover")->where(['related_index'=>$data['id'],'subject'=>10])->update(['amount'=>$data['id_card_fee']]);
                    Db::name("profit_loss_log")->where(['related_index'=>$data['id'],'subject'=>10])->update(['outcome_amount'=>$data['id_card_fee']]);
                }
			} catch (\Exception $e) {
				$this->error($e->getMessage());
			}
			return json(['status'=>'00','msg'=>'修改成功']);
		}
	}

    /*改为泰外交部认证状态*/
    function status() {
        if (!$this->request->isPost()){
            /*$id = $this->request->get('id','','intval');
            if(!$id) $this->error('参数错误');
            $id_card_certification_fee = Db::name("customer")->field('id_card_certification_fee')
                ->where([['status','>=',2],['id_card_certification_fee','>=',0]])->order('update_time','desc')->find();
            $pass_port_certification_fee = Db::name("customer")->field('pass_port_certification_fee')
                ->where([['status','>=',2],['pass_port_certification_fee','>=',0]])->order('update_time','desc')->find();

            //$id_card_fee =
            $this->view->assign('id_card_certification_fee',
                empty($id_card_certification_fee)?400:$id_card_certification_fee['id_card_certification_fee']);
            $this->view->assign('pass_port_certification_fee',
                empty($pass_port_certification_fee)?200:$pass_port_certification_fee['pass_port_certification_fee']);
            $this->view->assign('info',checkData(CustomerDb::getInfo($id)));*/
            $ids = $this->request->param('ids');
            $this->view->assign('ids',$ids);
            $this->view->assign('id_card_certification_fee',400);
            $this->view->assign('pass_port_certification_fee',200);
            return $this->display('stepone/status');
        }else{
            $postField = 'id,status,id_card_certification_fee,pass_port_certification_fee';
            $data = $this->request->only(explode(',',$postField),'post');
            //$info = CustomerDb::getInfo($data['id']);
            $where['id'] = $ids = explode(',',$data['id']);
            try {
                $now = time();
                Db::startTrans();
                unset($data['id']);
                CustomerService::editWhere($where,$data);
                $turnover = $profit_loss_log =[];
                foreach ($ids as $id) {
                    $data['id'] = $id;
                    $info = CustomerDb::getInfo($data['id']);
                    //损益表
                    $profit_id_card_certification_fee = [
                        'subject' => 30,
                        //'sub_subject' => '',
                        'income_amount' =>0,
                        'outcome_amount' =>$data['id_card_certification_fee'],
                        'related_index' =>$data['id'],
                        'created_time' => $now,
                        'created_d' => date('Ymd',$now),
                        'created_y' => date('Y',$now),
                        'created_ym' => date('Ym',$now),
                    ];
                    array_push($profit_loss_log,$profit_id_card_certification_fee);
                    $profit_pass_port_certification_fee = [
                        'subject' =>40,
                        //'sub_subject' => '',
                        'income_amount' =>0,
                        'outcome_amount' => $data['pass_port_certification_fee'],
                        'related_index' =>$data['id'],
                        'created_time' => $now,
                        'created_d' => date('Ymd',$now),
                        'created_y' => date('Y',$now),
                        'created_ym' => date('Ym',$now),
                    ];
                    array_push($profit_loss_log,$profit_pass_port_certification_fee);
                    $turnover_id_card_certification_fee = [
                        'type' =>1,
                        'sub_type' =>2,
                        'subject' =>30,
                        'director_id' =>$info['director_id'],
                        'introducer_id' =>$info['is_introducer'] ==1?$info['introducer_id']:0,
                        'customer_id' => $data['id'],
                        'customer_idcard' =>$info['id_card'],
                        'amount' => $data['id_card_certification_fee'],
                        'related_index' => $data['id'],
                        'created_time' => $now,
                        'created_d' => date('Ymd',$now),
                        'created_y' => date('Y',$now),
                        'created_ym' => date('Ym',$now),
                    ];
                    array_push($turnover,$turnover_id_card_certification_fee);
                    $turnover_pass_port_certification_fee = [
                        'type' =>1,
                        'sub_type' =>2,
                        'subject' =>40,
                        'director_id' =>$info['director_id'],
                        'introducer_id' =>$info['is_introducer'] ==1?$info['introducer_id']:0,
                        'customer_id' => $data['id'],
                        'customer_idcard' =>$info['id_card'],
                        'amount' =>  $data['pass_port_certification_fee'],
                        'related_index' => $data['id'],
                        'created_time' => $now,
                        'created_d' => date('Ymd',$now),
                        'created_y' => date('Y',$now),
                        'created_ym' => date('Ym',$now),
                    ];
                    array_push($turnover,$turnover_pass_port_certification_fee);
                    $customer_log[] = [
                        'c_id' => $data['id'],
                        'id_card' => $info['id_card'],
                        'status' => $data['status'],
                        'create_by' => session('admin.userid'),
                        'create_time' => $now,
                    ];
                }
                //p($turnover);p($profit_loss_log);
                Db::name("customer_log")->insertAll($customer_log);
                //流水表
                Db::name("turnover")->insertAll($turnover);
                Db::name("profit_loss_log")->insertAll($profit_loss_log);
                Db::commit();

            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                $this->error($e->getMessage());
            }
            return json(['status'=>'00','msg'=>'修改成功']);
        }
    }

	/*删除*/
	function delete(){
		$idx =  $this->request->post('ids', '', 'strip_tags');
		if(!$idx) $this->error('参数错误');
		try{
			$where['id'] = explode(',',$idx);
            CustomerService::delete($where);
		}catch(\Exception $e){
			$this->error($e->getMessage());
		}
		return json(['status'=>'00','msg'=>'操作成功']);
	}

	/*查看数据*/
	function view(){
		$id = $this->request->get('id','','intval');
		if(!$id) $this->error('参数错误');
		try{
			$this->view->assign('info',checkData(CustomerDb::getInfo($id)));
			return $this->display('record/customer/view');
		} catch (\Exception $e){
			$this->error($e->getMessage());
		}
	}



    function getThumbPic(){
        $detail = $this->request->post('detail','','strval');
        preg_match_all('/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/',$detail,$allImg);
        return json(['status'=>'00','imgurl'=>$allImg[1][0]]);
    }



}

